import  matplotlib.pyplot as plt
import  numpy as np
import math

delta = 0.1 #采样时间间隔
T = 10

#描述真实信号
t_0 = np.arange(0, T, 0.01)         #未采样前连续时间
XK = [0]*len(t_0)
for i in range(0,len(t_0)):
    XK[i] = t_0[i]+3

K = list(range(1,int(T/delta) + 1))
t = [0] * len(K)
wk = [0] * len(K)
x_measure = [0] * len(K)
e1 = [0] * len(K)
e2 = [0] * len(K)

for i in range (0, len(K)):
    t[i] = (K[i] - 1) * delta

for i in range(0,len(K)):
    wk[i] = np.random.normal(loc = 0.0, scale=5.0, size=None)
    XK[i] = t[i] + 3
    x_measure[i] = t[i] + 3 + wk[i]

x_hat = (1/len(K))*sum(x_measure)      #估计值

for i in range(0, len(K)):
    e1[i] = x_hat - x_measure[i]
    e2[i] = x_hat -(t[i] + 3)

plt.xlabel("Time(Sec)")
plt.ylabel("xhat")
plt.plot(t,x_measure,'-o',c = 'r',label = 'measures')
plt.axhline(y=x_hat, c="k",label = 'estimates',ls="--", lw=2)
plt.legend()
plt.show()

plt.xlabel("Time(Sec)")
plt.plot(t,e1,'-+',label = 'error between estimates and measures')
plt.plot(t,e2,c='r',label = 'error between estimates and true')
plt.legend()
plt.show()

plt.xlabel("Time(Sec)")
plt.axhline(y=x_hat, c="r",label = 'estimates',ls="--", lw=2)
plt.plot(t_0,XK,label = 'true')
plt.xlim(0,T)
plt.legend()
plt.show()